﻿using Microsoft.Xna.Framework;
using MoLingOverhaul.Common.Cores.Enum;
using MoLingOverhaul.Common.Systems;
using MoLingOverhaul.Content.Projectiles;
using System.Collections.Generic;
using System.Linq;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace MoLingOverhaul.Content.Seasons
{
    public class SpringNPC : GlobalNPC
    {
        public override bool InstancePerEntity => true;
        protected override bool CloneNewInstances => true;
        public override void ModifyShop(NPCShop shop)
        {
            var season = SeasonSystem.Instance.CurrentSeason.CurrentSeason;
            if (season == Season.Spring)
            {
                foreach (var entry in shop.Entries)
                {
                    entry.Item.value = (int)(entry.Item.value * 0.7f);
                }
            }
        }
        public override void EditSpawnPool(IDictionary<int, float> pool, NPCSpawnInfo spawnInfo)
        {
            var season = SeasonSystem.Instance.CurrentSeason.CurrentSeason;
            if (season == Season.Spring)
            {
                NPC npc = new();
                foreach (var npcType in pool.Keys.ToList())
                {
                    if (NPCID.Sets.TownCritter[npcType])
                    {
                        pool[npcType] *= 1.5f;
                    }
                    else if (!npc.friendly)
                    {
                        pool[npcType] *= 0.8f;
                    }
                }
            }
        }
    }
    public class SpringPlayer : ModPlayer
    {
        public bool hadLowHealth;
        public bool Leaf = false;
        public override void ResetEffects()
        {
            Leaf = false;
            hadLowHealth = false;
        }
        public override void UpdateDead()
        {
            Leaf = false;
            hadLowHealth = false;
        }
        public override void ModifyCaughtFish(Item fish)
        {
            var season = SeasonSystem.Instance.CurrentSeason.CurrentSeason;
            if (season == Season.Spring)
            {
                if (Main.rand.NextFloat() < 0.15f)
                {
                    fish.stack += 1;
                }
            }
        }
        public override void PostUpdate()
        {
            var season = SeasonSystem.Instance.CurrentSeason.CurrentSeason;
            if (season == Season.Spring)
            {
                float healthPercent = (float)Player.statLife / Player.statLifeMax2;
                Leaf = true;
                hadLowHealth = true;
                if (healthPercent <= 0.4f && hadLowHealth)
                {
                    if (Leaf)
                    {
                        for (int i = 0; i < 6; i++)
                        {
                            if (Main.myPlayer == Player.whoAmI)
                            {
                                Projectile.NewProjectile(Player.GetSource_FromThis(), Player.Center, Vector2.Zero, ModContent.ProjectileType<Leaf>(), 1, 2f, Player.whoAmI, ai0: i);
                            }
                        }
                    }
                }
            }
        }
        public class SpringTile : GlobalTile
        {
        }
    }
}